global fShutdown

; *****************************************************************************
;
; Pseudo-driver APM PM-32 bits 1.2 Compliant
; ( Sese, la implementacion del famoso SHUTDOWN ... )
;
; Descripcion: 1-Se establece version APM 1.2
;              2-Se habilita "Power Management" en todos los dispositivos
;              3-Se establece el "Power State" en "Off" para todos los disp.
; Parametros:  Ninguno
; Devuelve:    En caso de error, resultados en AX
;
; 2009 - By Shutdown Task Force
;        Solda Alejandro
;        Retamar Mario
;        Vazquez Walter
;
; Deliberately Omitted Accents And Enies
; Do Not Remove Credits, Don't Be Lamer
; Everything Else Is Allowed
; Have Fun
;
; For detailed information about APM read
; "Advanced Power Management (APM) BIOS Interface Specification"
; Revision 1.2
; February 1996
; Intel Corporation
; Microsoft Corporation
;
; *****************************************************************************

ALIGN 4

fShutdown:
   push  ebp
   mov   ebp, esp

   pushf
   push  ebx
   push  ecx
   push  edx

   ;; 4.6.15 APM Driver Version (0EH)
   ;; -------------------------------
   ;; The APM Driver uses this call to indicate its level of APM support to
   ;; the APM BIOS. The APM BIOS returns the APM connection version number.
   ;;
   ;; Call With
   ;;     AH = 53H APM
   ;;     AL = 0EH APM Driver Version
   ;;     BX = 0000H APM BIOS
   ;;     CH = APM Connection major version number (in BCD format)
   ;;     CL = APM Connection minor version number (in BCD format)
   ;;
   ;; Returns
   ;; If function successful:
   ;;     Carry = 0
   ;;     AH    = APM Driver major version number (in BCD format)
   ;;     AL    = APM Driver minor version number (in BCD format)
   ;; If function unsuccessful:
   ;;     Carry = 1
   ;;     AH    = Error code
   ;;             03H Interface not connected
   ;;             09H Unrecognized device ID
   ;;             0BH Interface not engaged
   ;;
   ; Le decimos al BIOS APM que "nosotros" somos "APM 1.2 compliant", o sea
   ; nos "autodeclaramos" como "driver APM 1.2".
   ; "Nosotros" somos el "driver APM del sistema operativo".
   mov   ax, 0x530e
   xor   bx, bx
   mov   cx, 0x0102
   call  0x30:0x0 
   jc    apm_end


   ;; 4.6.9 Enable/Disable Power Management (08H)
   ;; -------------------------------------------
   ;; This call enables or disables all APM BIOS automatic power
   ;; management. When disabled, the APM BIOS does not automatically
   ;; power manage devices, enter the Standby State, enter the Suspend
   ;; State, or take power saving steps in response to CPU Idle calls.
   ;;
   ;; Call With
   ;;     AH = 53H APM
   ;;     AL = 08H Enable/disable power management
   ;;     BX = Power device ID
   ;;          0001H All devices power managed by the APM BIOS
   ;;          *FFFFH All devices power managed by the APM BIOS
   ;;          All other values reserved
   ;;          * Must be implemented for compatibility with APM 1.0
   ;;     CX = Function code
   ;;          0000H Disable power management
   ;;          0001H Enable power management
   ;; Returns
   ;; If function successful:
   ;;     Carry = 0
   ;; If function unsuccessful:
   ;;     Carry = 1
   ;;     AH    = Error code
   ;;             03H Interface not connected
   ;;             09H Unrecognized device ID
   ;;             0AH Parameter value out or range (function code)
   ;;             0BH Interface not engaged
   ;;
   mov   ax, 0x5308
   mov   bx, 0x0001
   mov   cx, 0x0001
   call  0x30:0x0 
   jc    apm_end

   ;; 4.6.8 Set Power State (07H)
   ;; ---------------------------
   ;; This call sets the system or device specified in the power device ID
   ;; into the requested power state.
   ;;
   ;; Call With
   ;;     AH = 53H APM
   ;;     AL = 07H Set Power State
   ;;     BX = Power device ID
   ;;          0001H All devices power managed by the APM BIOS
   ;;          01XXH Display
   ;;          02XXH Secondary Storage
   ;;          03XXH Parallel Ports
   ;;          04XXH Serial Ports
   ;;          05XXH Network Adapters
   ;;          06XXH PCMCIA Sockets
   ;;          E000H - EFFFH OEM-defined Power Device IDs
   ;;          All other values reserved
   ;;          where:
   ;;             XX H = Unit Number (0 based)
   ;;             Unit Number of FF H means all devices in this class
   ;;     CX = Power state
   ;;          *0000H       APM Enabled
   ;;          0001H        Standby
   ;;          0002H        Suspend
   ;;          0003H        Off
   ;;          **0004H      Last Request Processing Notification
   ;;          **0005H      Last Request Rejected
   ;;          0006H-001FH  Reserved system states
   ;;          0020H-003FH  OEM-defined system states
   ;;          0040H-007FH  OEM-defined device states
   ;;          0080H-FFFFH  Reserved device states
   ;;          * Not supported for Power Device ID 0001H
   ;;          ** Only supported for Power Device ID 0001H
   ;;
   ;; Returns:
   ;; If function successful:
   ;; Carry = 0
   ;; If function unsuccessful:
   ;; Carry = 1
   ;; AH    = Error code
   ;;         01H Power management functionality disabled
   ;;         03H Interface not connected
   ;;         09H Unrecognized device ID
   ;;         0AH Parameter value out of range
   ;;         0BH Interface not engaged
   ;;         60H Unable to enter requested state
   ;;
   mov   ax, 0x5307
   mov   bx, 0x0001
   mov   cx, 0x0003
   call  0x30:0x0 
   jc    apm_end

apm_end:
   ; Si llegamos aca hubo error en alguna de las llamada al BIOS APM.
   ; Nos queda:
   ; AL funcion que pincho
   ; AH el codigo de error

   pop   edx   
   pop   ecx   
   pop   ebx   
   popf

   mov   esp, ebp
   pop   ebp
   ret
